﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FileDFS.Utils.PathConfig;
using FileDFS.Model;
using FileDFS.Utils;
using System.Drawing;
using FileDFS.Model.Enum;

namespace FileDFS.Bll
{
    /*
     * Coder:Tieria
     * WriteTime:2015.5.13
     * FileName:FileExtensionEnum.cs
     * ReMark:文件类型枚举,因为限制,只能上传几种类型,不在此范围的将不能上传
     */
    public class DownloadHandler
    {
        public static DownloadHandler GetInstance()
        {
            return new DownloadHandler();
        }

        #region 下载单个文件,包括图片和文件
        /// <summary>
        /// 下载单个文件
        /// </summary>
        /// <param name="virtualFullPath"></param>
        /// <returns></returns>
        public DownloadResult DownloadSingle(DownloadModel downloadModel)
        {
            DownloadResult result = new DownloadResult()
            {
                DownloadId=downloadModel.DownloadId
            };
            try
            {
                PathDetail pathDetail = CatalogHandler.GetInstance().GetDetailByVirtual(downloadModel.VirtualFullPath);
                result.ExtensionName = PathHelper.GetExtensionByFullPath(downloadModel.VirtualFullPath);
                Stream stream = new MemoryStream();
                FileStream fs = new FileStream(pathDetail.Absolute_FullPath, FileMode.Open, FileAccess.Read);
                fs.CopyTo(stream);
                stream.Position = 0;
                result.DataSource = stream;

                fs.Flush();
                fs.Close();
            }
            catch
            {

            }
            return result;
        } 
        #endregion

        #region 下载某个图片的缩略图
        public ThumbnailDownloadResult DownloadThumbnail(ThumbnailDownloadModel downloadModel)
        {
            ThumbnailDownloadResult result = new ThumbnailDownloadResult()
            {
                DownloadId = downloadModel.DownloadId,
                DownloadPath = downloadModel.VirtualFullPath,
                Extension=ImageExtensionEnum.jpeg
            };
            try
            {
                PathDetail pathDetail = CatalogHandler.GetInstance().GetDetailByVirtual(downloadModel.VirtualFullPath);
                Image originalImage = Image.FromFile(pathDetail.Absolute_FullPath);
                ImageZoomContext context = new ImageZoomContext()
                {
                    Mode = ImageCutModeEnum.Auto,
                    SourceImage = originalImage,
                    ZoomWidth = downloadModel.Width,
                    ZoomHight = downloadModel.Height
                };

                context = ImageHelper.GetInstance().GetSize(context);

                //缩略图
                Image image = ImageHelper.GetInstance().GetZoomImage(context);
                Stream stream = new MemoryStream();
                image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                stream.Position = 0;
                result.DataSource = stream;
            }
            catch (Exception ex)
            {
                Utils.TestLog.Logger(ex.Message);
            }

            return result;
        }
        #endregion
         

    }
}
